﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using DTO;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace DAL
{
    public class BAI_VIETDAL:KetNoiDuLieu
    {
        public void Insert(BAI_VIET baiviet)
        {
            SqlCommand cm = new SqlCommand("sp_baiviet_Insert", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA_LOAI_BV", baiviet.Maloaibv);
            cm.Parameters.AddWithValue("@TEN_BAI_VIET", baiviet.Tenbv);            
            cm.Parameters.AddWithValue("@NOI_DUNG", baiviet.Noidung);
            cm.Parameters.AddWithValue("@TRANG_THAI", baiviet.Trangthai);
            cm.ExecuteNonQuery();
        }

        public void Update(BAI_VIET baiviet)
        {
            SqlCommand cm = new SqlCommand("sp_baiviet_Update", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA_BV", baiviet.Mabv);            
            cm.Parameters.AddWithValue("@MA_LOAI_BV", baiviet.Maloaibv);
            cm.Parameters.AddWithValue("@TEN_BAI_VIET", baiviet.Tenbv);
            cm.Parameters.AddWithValue("@NOI_DUNG", baiviet.Noidung);
            cm.Parameters.AddWithValue("@TRANG_THAI", baiviet.Trangthai);
            cm.ExecuteNonQuery();
        }

        public void Update_status(BAI_VIET baiviet)
        {
            SqlCommand cm = new SqlCommand("Update BAI_VIET SET TRANG_THAI = " + baiviet.Trangthai.ToString() + " WHERE MA_BV = " + baiviet.Mabv, LayKetNoi());
            cm.ExecuteNonQuery();
        }

        public void Delete(int mabv)
        {
            SqlCommand cm = new SqlCommand("sp_baiviet_Delete", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA_BV", mabv);
            cm.ExecuteNonQuery();
        }

        public Baivetcollection Get(string Where, int PageIndex, int PageSize, string OrderBy, string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_baiviet_GET", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);
            SqlParameter param = new SqlParameter("@TotalRecords", DbType.Int32);
            param.Direction = ParameterDirection.Output;
            cm.Parameters.Add(param);
            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Baivetcollection baivietcollection = new Baivetcollection();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                BAI_VIET bv = new BAI_VIET();
                bv.Mabv = int.Parse(ds.Tables[0].Rows[i]["MA_BV"].ToString());
                bv.Maloaibv = int.Parse(ds.Tables[0].Rows[i]["MA_LOAI_BV"].ToString());
                bv.Tenbv = ds.Tables[0].Rows[i]["TEN_BAI_VIET"].ToString();
                bv.Noidung = ds.Tables[0].Rows[i]["NOI_DUNG"].ToString();
                bv.Trangthai = int.Parse(ds.Tables[0].Rows[i]["TRANG_THAI"].ToString());
                baivietcollection.Add(bv);
            }
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return baivietcollection;
        }
    }
}
